/**
 * @FileName: JointParams.java
 * @Author
 * @Description:
 * @Date 2017年2月9日 下午3:11:01
 * @CopyRight ZTE Corporation /**
 * @FileName: Params.java
 * @Author
 * @Description:
 * @Date 2017年1月10日 下午3:57:12
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.util;

import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.kit.StrKit;

/**
 * 对加密后参数串进行解析和获取。key=value|key=value|key=value，原串。
 *
 */
public class JointParams{
    public static final String                DES               = "DES";
    private static final Logger               logger            = LoggerFactory.getLogger(JointParams.class);
    private static final String               DEFAULT_SEPERATOR = "\\|";
    private ConcurrentHashMap<String, String> map               = new ConcurrentHashMap<String, String>();

    /**
     * 该方法用于1.0app的选号入网后台业务办理接口。待切换2.0后，可以丢弃
     * 
     * @throws Exception
     */
    public JointParams(String method, String key, String paramsBefore) throws Exception{
        if(!method.equals(DES)){
            // 不解析，后续获取什么key的value都会为空
            return;
        }
        // logger.info("class desc params = " + paramsBefore);
        DESAS desas = new DESAS(key);
        String params = desas.decrypt(paramsBefore);
        // logger.info("decoded params = " + params);
        parseMap(params, DEFAULT_SEPERATOR);

    }

    public String getPara(String key){
        String value = map.get(key);
        // key不存在的，返回空
        if(value == null){
            return "";
        }
        return value;
    }

    /**
     * @Title: parseMap
     * @Description:TODO
     * @param params
     *            void
     * @param seperator
     *            TODO
     */

    private void parseMap(String params, String seperator){
        if(StrKit.isBlank(params)){
            // 直接退出去，空map后续什么也获取不到
            logger.warn("params  is blank.");
            return;
        }

        String[] array = params.split(seperator);
        for(int i = 0; i < array.length; i++){
            String keyValue = array[i];
            // logger.info(keyValue);
            if(StrKit.isBlank(keyValue)){
                // 跳过
                logger.warn("keyValue is blank:" + keyValue);
                continue;
            }
            String[] keyValueArray = keyValue.split("=");
            String keyString = "";
            String valueString = "";
            if(keyValueArray.length > 0){
                keyString = keyValueArray[0];
            } else{
                // 非法数据，跳过
                logger.warn("keyValue is ilegal:" + keyValue + ":" + keyValueArray.length);
                continue;
            }

            if(keyValueArray.length == 1){
                // a=这种情况
                valueString = "";
            } else if(keyValueArray.length == 2){
                // a=x这种情况
                valueString = keyValueArray[1];
            } else{
                // 其他情况 a=x=y，跳过
                logger.warn("keyValue is ilegal:" + keyValue);
                continue;
            }
            map.put(keyString, valueString);
        }
        // 调试
        // System.out.println("param str:");
        // for(Iterator iter = map.keySet().iterator(); iter.hasNext();){
        // String name = (String)iter.next();
        // String values = map.get(name);
        //
        // System.out.print(name + "=" + values + "|");
        //
        // }
        // System.out.println("");
        // System.out.println("param str end");
    }

}
